package cn.jlkj.csmall.product1901.teacher.service.impl;


import cn.jlkj.csmall.product1901.teacher.ex.ServiceException;
import cn.jlkj.csmall.product1901.teacher.mapper.AlbumMapper;
import cn.jlkj.csmall.product1901.teacher.pojo.dto.AlbumAddNewDTO;
import cn.jlkj.csmall.product1901.teacher.pojo.entity.Album;
import cn.jlkj.csmall.product1901.teacher.service.IAlbumService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AlbumServiceImpl implements IAlbumService {

    @Autowired
    private AlbumMapper albumMapper;


    public AlbumServiceImpl() {
        log.debug("创建业务对象：AlbumServiceImpl");
    }


    @Override
    public void addNew(AlbumAddNewDTO albumAddNewDTO) {
        // 应该保证此相册的名称是唯一的
        String albumName = albumAddNewDTO.getName();
        int count = albumMapper.countByName(albumName);
        if (count > 0) {
            throw new ServiceException();
        }

        // 创建Album类型的对象
        Album album = new Album();
        // 调用BeanUtils.copyProperties(源对象, 目标对象)将参数的属性值复制到新创建的Album对象中
        BeanUtils.copyProperties(albumAddNewDTO, album);
        // 调用albumMapper的int insert(Album album)方法插入相册数据
        albumMapper.insert(album);
    }
}
